<?php

namespace App\Console\Commands;

use App\Events\RepayMentEvent;
use App\Facades\MoneyPlatformRequestFacade;
use App\Library\Guzzle\Sign;
use App\Models\RepayRecord;
use App\Models\RepayRecordCommon;
use App\Models\RepayRecordMoney;
use App\Models\RepayRecordMonth;
use App\Models\RepaySummary;
use App\Models\Traits\Repay;
use App\Models\UserGlobal;
use Illuminate\Console\Command;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use App\Models\Traits\RepaySummary as RepaySummaryModel;

class RepairCollectionCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'repair:collection {action : test|updateCase|repairSettle|settle} {--loan_id=} {--summary_id=} {--start_date=}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'repair collection command';

    /**
     * The console command action.
     *
     * @var string
     */
    protected $action;

    /**
     * Create a new command instance.
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     */
    public function handle()
    {
        $this->initAction();
        $this->runAction();
    }

    protected function initAction()
    {
        $this->action = $this->argument('action');

        if (!in_array($this->action, ['test', 'updateCase','repairSettle','settle'])) {
            $this->error("Invalid argument '{$this->action}'. Expected 'test', 'updateCase'.");
            exit(1);
        }
    }

    protected function runAction()
    {
        $this->{$this->action}();
    }

    /**
     * Description: 修复更新分案后还款信息
     * Author: Gxs
     */
    public function updateCase()
    {
        $summary_id = $this->option('summary_id');
        $start_date = $this->option('start_date');

        DB::table('repay_records')
            ->select(DB::raw('DISTINCT summary_id'))
            ->when($summary_id, function ($query) use ($summary_id) {
                $query->where('summary_id', $summary_id);
            })
            ->when($start_date, function ($query) use ($start_date){
                $query->where('repay_date', '>=', $start_date);
            })
            ->orderBy('summary_id', 'asc')
            ->chunk(2000, function ($summaries) {
                foreach ($summaries as $summary) {
                    //通知催收系统更新分案后还款信息
                    event(new RepayMentEvent($summary->summary_id));
                    $this->info($summary->summary_id . '已触发事件');
                }
            });
        $this->info('repair collection test');
    }
    /*
     * 线下结清
     */
    public function settle()
    {
        $loan_id = $this->option('loan_id'); //获取loan_id
        $summaryInfo = RepaySummary::select(['id'])->where('loan_id',$loan_id)->first();
        if(empty($summaryInfo)){
            Log::debug('offline_settle_fail_status',['loan_id'=>$loan_id,'msg'=>'没有这条订单']);
            return false;
        }
        $now=Carbon::now()->format('Y-m-d');
        $info_one=RepayRecord::where('summary_id',$summaryInfo->id)
            ->where('period',0)
            ->where('status',4)
            ->first();
        if($info_one){
            Log::debug('offline_settle_fail_status',['loan_id'=>$loan_id,'msg'=>'已经完成']);
            return false;
        }
        $info_two=RepayRecord::where('summary_id',$summaryInfo->id)
            ->where('period',0)
            ->where('status','<>',4)
            ->first();
        DB::beginTransaction();
        try {
            if ($info_two) {
                $trade_no=\App\Models\Traits\RepaySummary::createTradeNo($info_two->uid,7);
                $result = $this->setMoney($info_two,$trade_no);
                if ($result['code'] == '01') {
                    //创建第0期
                    if($result['data']['operateStatus']=='R'){
                        $pay_type=1;
                    }elseif ($result['data']['operateStatus']=='J'){
                        $pay_type=7;
                    }elseif ($result['data']['operateStatus']=='T') {
                        $pay_type = 6;
                    }
                    if($result['data']['resultStatus']=='S'){
                        $status=4;
                    }elseif ($result['data']['resultStatus']=='P'){
                        $status=2;
                    }elseif('F'==$result['data']['resultStatus']){
                        $status=3;
                    }
                    $types=RepayRecordMoney::$third_map_type;
                    $type=empty($result['data']['resultPlanStatus'])?0:$types[$result['data']['resultPlanStatus']];
                    //获取总期数
                    $count=RepayRecord::where('summary_id',$info_two->summary_id)->count();
                    //把0期修改成功
                    RepayRecord::where('id', $info_two->id)
                        ->update([
                            'amount'=>bcmul($result['data']['principalAmt'],100),
                            'status' => $status,
                            'pay_off' => 2,
                            'repay_date' => $now,
                            'repay_type' => $pay_type
                        ]);
                    RepayRecordMoney::where('period', 0)
                        ->where('record_id', $info_two->id)
                        ->update([
                            'third_repay_id' => $result['data']['resultRepayId'],
                            'trade_no' => $trade_no,
                            'lending_money' => $result['data']['interestAmt'],
                            'fines_money' => $result['data']['penaltyAmt'],
                            'repayment_money' => $result['data']['serviceAmt'],
                            'failure'=>$result['data']['resultRemark'],
                            'type' => $type
                        ]);
                    RepayRecordMonth::where('period', 0)->where('record_id', $info_two->id)
                        ->update(['third_record_id' => '', 'trade_no' => '', 'lending_money' => '', 'fines_money' => '']);
                    $summary_data=RepaySummary::where('id',$info_two->summary_id)->first();
                    $moneyData=RepaySummaryModel::moneyData($info_two->summary_id,$summary_data->loan_id,$info_two);
                    $common_data=RepaySummaryModel::commonData($info_two->summary_id,$summary_data,$info_two,$moneyData);

                    RepayRecordCommon::where('period', 0)->where('record_id', $info_two->id)
                        ->update([
                            'third_record_id' => '',
                            'trade_no' => '',
                            'repayment_money'=>$common_data['repayment_money'],
                        ]);
                    //修改其他期状态
                    $resultRepayDate=empty($result['data']['resultRepayDate'])?Carbon::now()->format('Y-m-d'):$result['data']['resultRepayDate'];
                    for($i=$result['data']['currentPeriod'];$i<=$count;$i++){
                        RepayRecord::where('summary_id',$info_two->summary_id)
                            ->where('period',$i)
                            ->update([
                                'status'=>4,
                                'pay_off'=>2,
                                'repay_type'=>7,
                                'repay_date'=>$resultRepayDate
                            ]);
                        RepayRecordMoney::where('summary_id',$info_two->summary_id)
                            ->where('period',$i)
                            ->update([
                                'status'=>4,
                                'repay_type'=>7,
                                'type'=>$type,
                                'repay_date'=>$resultRepayDate
                            ]);
                        RepayRecordMonth::where('summary_id',$info_two->summary_id)
                            ->where('period',$i)
                            ->update([
                                'status'=>4,
                                'repay_type'=>7,
                                'apply_time' => Carbon::now(), //实际还款日期
                                'status_time' => Carbon::now(), //实际还款日期
                            ]);
                        RepayRecordCommon::where('summary_id',$info_two->summary_id)
                            ->where('period',$i)
                            ->update([
                                'status'=>4,
                                'repay_type'=>7,
                                'apply_time' => Carbon::now(), //实际还款日期
                                'status_time' => Carbon::now(), //实际还款日期
                            ]);
                    }
                    Repay::setAllPeriodSuccess($info_two, RepayRecord::REPAY_TYPE_XIANXIA, false);
                    //修改全局状态
                    UserGlobal::where('uid', $info_two->uid)->update(['global_status' => UserGlobal::STATUS_REPAY_OVER]); //全局状态
                    DB::commit();
                    event(new RepayMentEvent($info_two->id));
                }
            } else {
                //第一次请求全部结清
                $info_three = RepayRecord::where('summary_id', $summaryInfo->id)->first();
                $trade_no=\App\Models\Traits\RepaySummary::createTradeNo($info_three->uid,7);
                $result = $this->setMoney($info_three,$trade_no);
                LOG::debug('还款结果',['result'=>$result]);
                if ($result['code'] == '01') {
                    //创建第0期
                    if($result['data']['operateStatus']=='R'){
                        $pay_type=1;
                    }elseif ($result['data']['operateStatus']=='J'){
                        $pay_type=7;
                    }
                    if($result['data']['resultStatus']=='S'){
                        $status=4;
                    }elseif ($result['data']['resultStatus']=='P'){
                        $status=2;
                    }elseif('F'==$result['data']['resultStatus']){
                        $status=3;
                    }
                    $failure_status=0;
                    if($result['data']['resultRemark']&&$result['data']['resultRemark']!='success'){
                        $failure_status=3;
                    }
                    $types=RepayRecordMoney::$third_map_type;
                    $type=empty($result['data']['resultPlanStatus'])?0:$types[$result['data']['resultPlanStatus']];
                    //获取总期数
                    $count=RepayRecord::where('summary_id',$info_three->summary_id)->count();
                    //record表创建第0条
                    $record_id=RepayRecord::insertGetId([
                        'uid'=>$info_three->uid,
                        'summary_id'=>$info_three->summary_id,
                        'bank_cards_id'=>$info_three->bank_cards_id,
                        'period'=>0,
                        'amount'=>bcmul($result['data']['principalAmt'],100),
                        'status' => $status,
                        'failure_status'=>$failure_status,
                        'failure_message'=>$result['data']['resultRemark'],
                        'pay_off' => 2,
                        'repay_date' => empty($result['data']['resultRepayDate'])?$now:$result['data']['resultRepayDate'],
                        'created_at'=>$now,
                        'updated_at'=>$now,
                        'repay_type' => $pay_type
                    ]);
                    //money表创第0条
                    RepayRecordMoney::create([
                        'uid'=>$info_three->uid,
                        'summary_id'=>$info_three->summary_id,
                        'record_id'=>$record_id,
                        'bank_cards_id'=>$info_three->bank_cards_id,
                        'third_repay_id' => $result['data']['resultRepayId'],
                        'trade_no' => $trade_no,
                        'lending_money' => $result['data']['interestAmt'],
                        'fines_money' =>$result['data']['penaltyAmt'],
                        'repayment_money' =>$result['data']['serviceAmt'],
                        'failure'=>$result['data']['resultRemark'],
                        'status'=>$status,
                        'type' => $type,
                        'status_time'=>$now,
                        'repay_type'=>$pay_type,
                        'created_at'=>$now,
                    //    'updated_at'=>$now, 
                    ]);

                    //month表创建第0条
                    RepayRecordMonth::create([
                        'uid'=>$info_three->uid,
                        'summary_id'=>$info_three->summary_id,
                        'record_id'=>$record_id,
                        'bank_cards_id'=>$info_three->bank_cards_id,
                        'third_record_id' => '',
                        'trade_no' => '',
                        'amount'=>0,
                        'period'=>0,
                        'status'=>$status,
                        'status_time'=>$now,
                        'repay_type'=>$pay_type,
                        'created_at'=>$now,
                     //   'updated_at'=>$now, 
                    ]);

                    $summary_data=RepaySummary::where('id',$info_three->summary_id)->first();
                    $moneyData=$this->moneyData($info_three->summary_id,$summary_data->loan_id,$info_three);
                    $common_data=$this->commonData($info_three->summary_id,$summary_data,$info_three,$moneyData);
                    //common表创建第0条
                    RepayRecordCommon::create([
                        'uid'=>$info_three->uid,
                        'summary_id'=>$info_three->summary_id,
                        'record_id'=>$record_id,
                        'bank_cards_id'=>$info_three->bank_cards_id,
                        'third_record_id' => '',
                        'trade_no' => '',
                        'period'=>0,
                        'lending_money' => 0,
                        'fines_money' => 0,
                        'repayment_money'=>$common_data['repayment_money'],
                        'status'=>4,
                        'status_time'=>$now,
                        'repay_type'=>$pay_type,
                        'created_at'=>$now,
                     //   'updated_at'=>$now, 
                    ]);
                    //修改其他期状态
                    RepaySummary::where('id',$info_three->summary_id)->update([
                        'period'=>$count,
                        'status'=>2
                    ]);
                    $resultRepayDate=empty($result['data']['resultRepayDate'])?Carbon::now()->format('Y-m-d'):$result['data']['resultRepayDate'];
                    for($i=$result['data']['currentPeriod'];$i<=$count;$i++){
                        RepayRecord::where('summary_id',$info_three->summary_id)
                            ->where('period',$i)
                            ->update([
                                'status'=>4,
                                'pay_off'=>2,
                                'repay_type'=>7,
                                'repay_date'=>$resultRepayDate
                            ]);
                        RepayRecordMoney::where('summary_id',$info_three->summary_id)
                            ->where('period',$i)
                            ->update([
                                'status'=>4,
                                'repay_type'=>7,
                                'repay_date'=>$resultRepayDate
                            ]);
                        RepayRecordMonth::where('summary_id',$info_three->summary_id)
                            ->where('period',$i)
                            ->update([
                                'status'=>4,
                                'repay_type'=>7,
                                'apply_time' => Carbon::now(), //实际还款日期
                                'status_time' => Carbon::now(), //实际还款日期
                            ]);
                        RepayRecordCommon::where('summary_id',$info_three->summary_id)
                            ->where('period',$i)
                            ->update([
                                'status'=>4,
                                'repay_type'=>7,
                                'apply_time' => Carbon::now(), //实际还款日期
                                'status_time' => Carbon::now(), //实际还款日期
                            ]);
                    }
                    //Repay::setAllPeriodSuccess($info_three, RepayRecord::REPAY_TYPE_XIANXIA, false);
                    //修改全局状态
                    UserGlobal::where('uid', $info_three->uid)->update(['global_status' => UserGlobal::STATUS_REPAY_OVER]); //全局状态
                    DB::commit();
                    //同步催收
                    event(new RepayMentEvent($info_three->id));
                }
            }
            $this->info('success');
        }catch (\Exception $e){
            Log::debug('settle_money', ['message' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine()]);
            DB::rollBack();
            return false;
        }
    }

    /**
     * Description:请求资方接口
     * Author:lijiafei
     * @param $recordInfo RepayRecord 表的model
     * @param $tradeNo 1 逐期还款 2 全部还清
     * @return bool
     */
    private function setMoney($recordInfo,$trade_no){
        $questData = $this->getMoneyRequestData($recordInfo);
        if(!$questData){
            return false;
        }
        $moneyData['loanId'] = $recordInfo->loan_id;
//        $moneyData['merOrderNo'] = $recordInfo->trade_no;
        $moneyData['merOrderNo'] = $trade_no;
        $moneyData['currentPeriod'] = $questData['period'];
        $moneyData['totalAmt'] = $questData['totalAmt']; //还款总金额
        $moneyData['principalAmt'] = $questData['amount']; //本金
        $moneyData['interestAmt'] = $questData['lending_money']; //利息
        $moneyData['serviceAmt'] = $questData['repayment_money']; //服务费
        $moneyData['penaltyAmt'] = $questData['fines_money']; //罚息
        $moneyData['operateStatus'] = $questData['operateStatus'];
        $data_type=RepaySummary::where('id',$recordInfo->summary_id)->select('data_type')->first();
        $res = MoneyPlatformRequestFacade::setMoneyRepay($moneyData,$data_type->data_type);
//        $res = MoneyPlatformRequestFacade::setMoneyRepay($moneyData,1);
//        return true;
        return $res;
    }

    /**
     * 封装资方的接口请求数据
     * @param $recordInfo RepayRecord repay_record的model
     * @return  array|boolean data
     */
    protected function getMoneyRequestData($recordInfo){
        //提前结清
        $operateStatus = 'J';
        $period = RepayRecord::where('summary_id',$recordInfo->summary_id)->where('pay_off',RepayRecord::PAY_OFF_ONE)->where('status','<>',RepayRecord::STATUS_FOUR)->orderBy('period','asc')->value('period');
        $data = MoneyPlatformRequestFacade::getRepaymentPlan($recordInfo->loan_id);
        if(empty($data['data'])){
            return false;
        }else{
            $data = $data['data'];
            $total = $data['totalAmount'];
            $amount = $data['principalAmount'];
            $lending_money = $data['interestAmount'];
            $repayment_money = $data['serviceAmount'];
            $fines_money = $data['penaltyAmount'];
        }
        $data = [
            'period' => $period,
            'totalAmt' => $total,//总金额
            'amount' => $amount,//本金
            'lending_money' => $lending_money,//利息
            'repayment_money' => $repayment_money,//提前结清手续费
            'fines_money' => $fines_money,//罚息
            'operateStatus' => $operateStatus,
        ];
        return $data;
    }
    /*
    * 修改线下全部结清
    */
    public function repairSettle()
    {
        $data=[
            'LO201903131703220609882384280241'=>[
                                                    'third_repay_id'=>'RR201903202134247861839523025916',
                                                    'principalAmt'=>15000.0,
                                                    'penaltyAmt'=>0.0,
                                                    'interestAmt'=>23.47,
                                                    'serviceAmt'=>0.0,
                                                    'totalAmt'=>15023.47,
                                                    'resultRepayDate'=>'2019-03-20 21:34'
                                                ],
            'LO201903111706573729164963392165'=>[
                                                    'third_repay_id'=>'RR201903202137117241590247107697',
                                                    'principalAmt'=>4000.0,
                                                    'penaltyAmt'=>0.0,
                                                    'interestAmt'=>8.05,
                                                    'serviceAmt'=>0.0,
                                                    'totalAmt'=>4008.05,
                                                    'resultRepayDate'=>'2019-03-20 21:37'
                                                ],
            'LO201902220108133859619820265223'=>[
                                                    'third_repay_id'=>'RR201903251609346331397070396390',
                                                    'principalAmt'=>15000.0,
                                                    'penaltyAmt'=>253.43,
                                                    'interestAmt'=>460.9,
                                                    'serviceAmt'=>0.0,
                                                    'totalAmt'=>15714.33,
                                                    'resultRepayDate'=>'2019-03-25 15:31'
                                                ],
            'LO201902220949382999135924143955'=>[
                                                    'third_repay_id'=>'RR201903250923122781701227166063',
                                                    'principalAmt'=>2534.26,
                                                    'penaltyAmt'=>50.69,
                                                    'interestAmt'=>7.18,
                                                    'serviceAmt'=>0.0,
                                                    'totalAmt'=>2592.13,
                                                    'resultRepayDate'=>'2019-03-25 15:31'
                                                ],
            'LO201902220108189809246549250382'=>[
                                                    'third_repay_id'=>'RR201903251609346331397070396366',
                                                    'principalAmt'=>15000.0,
                                                    'penaltyAmt'=>253.43,
                                                    'interestAmt'=>460.9,
                                                    'serviceAmt'=>0.0,
                                                    'totalAmt'=>15714.33,
                                                    'resultRepayDate'=>'2019-03-25 15:31'
                                                ],
            'LO201902220950002629743771122638'=>[
                                                    'third_repay_id'=>'RR201903251609346331397070396110',
                                                    'principalAmt'=>3000.0,
                                                    'penaltyAmt'=>50.69,
                                                    'interestAmt'=>92.18,
                                                    'serviceAmt'=>0.0,
                                                    'totalAmt'=>3142.87,
                                                    'resultRepayDate'=>'2019-03-25 15:31'
                                                ],
            'LO201902210100299449104686420752'=>[
                                                    'third_repay_id'=>'RR201903251609346331397070396321',
                                                    'principalAmt'=>15000.0,
                                                    'penaltyAmt'=>223.92,
                                                    'interestAmt'=>463.3,
                                                    'serviceAmt'=>0.0,
                                                    'totalAmt'=>15687.22,
                                                    'resultRepayDate'=>'2019-03-25 15:31'
                                                ],
            'LO201903081319225159773165006429'=>[
                                                    'third_repay_id'=>'RR201903251609346331397070396396',
                                                    'principalAmt'=>7000.0,
                                                    'penaltyAmt'=>140.0,
                                                    'interestAmt'=>112.39,
                                                    'serviceAmt'=>0.0,
                                                    'totalAmt'=>7252.39,
                                                    'resultRepayDate'=>'2019-03-25 15:31'
                                                ],
            'LO201903131433439129451624869857'=>[
                                                    'third_repay_id'=>'RR201903251609346331397070396392',
                                                    'principalAmt'=>15000.0,
                                                    'penaltyAmt'=>300.0,
                                                    'interestAmt'=>113.33,
                                                    'serviceAmt'=>0.0,
                                                    'totalAmt'=>15413.33,
                                                    'resultRepayDate'=>'2019-03-25 15:31'
                                                ],
            'LO201903121606384249518718432855'=>[
                                                    'third_repay_id'=>'RR201904091947518481536066704135',
                                                    'principalAmt'=>2006.68,
                                                    'penaltyAmt'=>0.0,
                                                    'interestAmt'=>0.0,
                                                    'serviceAmt'=>0.0,
                                                    'totalAmt'=>2006.68,
                                                    'resultRepayDate'=>'2019-04-09 19:47'
                                                ],
            'LO201903251916278839713081788001'=>[
                                                    'third_repay_id'=>'RR201903281831136881767576766440',
                                                    'principalAmt'=>15000.0,
                                                    'penaltyAmt'=>0.0,
                                                    'interestAmt'=>10.06,
                                                    'serviceAmt'=>0.0,
                                                    'totalAmt'=>15010.06,
                                                    'resultRepayDate'=>'2019-03-28 18:31'
                                                ],
            'LO201903251226093229746777309124'=>[
                                                    'third_repay_id'=>'RR201903281832333331774498666022',
                                                    'principalAmt'=>12000.0,
                                                    'penaltyAmt'=>0.0,
                                                    'interestAmt'=>8.05,
                                                    'serviceAmt'=>0.0,
                                                    'totalAmt'=>12008.05,
                                                    'resultRepayDate'=>'2019-03-28 18:32'
                                                ],
            'LO201903251120425789588824791353'=>[
                                                    'third_repay_id'=>'RR201903281834137041537647502660',
                                                    'principalAmt'=>12000.0,
                                                    'penaltyAmt'=>0.0,
                                                    'interestAmt'=>8.05,
                                                    'serviceAmt'=>0.0,
                                                    'totalAmt'=>12008.05,
                                                    'resultRepayDate'=>'2019-03-28 18:34'
                                                ],
            'LO201903242048182339399837636905'=>[
                                                    'third_repay_id'=>'RR201903281835397211532559142007',
                                                    'principalAmt'=>12000.0,
                                                    'penaltyAmt'=>0.0,
                                                    'interestAmt'=>10.73,
                                                    'serviceAmt'=>0.0,
                                                    'totalAmt'=>12010.73,
                                                    'resultRepayDate'=>'2019-03-28 18:35'
                                                ],
            'LO201903241951236259558369831263'=>[
                                                    'third_repay_id'=>'RR201903281836524111751059928956',
                                                    'principalAmt'=>5000.0,
                                                    'penaltyAmt'=>0.0,
                                                    'interestAmt'=>4.47,
                                                    'serviceAmt'=>0.0,
                                                    'totalAmt'=>5004.47,
                                                    'resultRepayDate'=>'2019-03-28 18:36'
                                                ],
            'LO201903241602365829302718881275'=>[
                                                    'third_repay_id'=>'RR201903281837330651289987798053',
                                                    'principalAmt'=>15000.0,
                                                    'penaltyAmt'=>0.0,
                                                    'interestAmt'=>13.41,
                                                    'serviceAmt'=>0.0,
                                                    'totalAmt'=>15013.41,
                                                    'resultRepayDate'=>'2019-03-28 18:37'
                                                ],
            'LO201903241441063519643376924188'=>[
                                                    'third_repay_id'=>'RR201903281838302861325131190195',
                                                    'principalAmt'=>2000.0,
                                                    'penaltyAmt'=>0.0,
                                                    'interestAmt'=>1.79,
                                                    'serviceAmt'=>0.0,
                                                    'totalAmt'=>2001.79,
                                                    'resultRepayDate'=>'2019-03-28 18:38'
                                                ],
            'LO201903240023566619298095594370'=>[
                                                    'third_repay_id'=>'RR201903281839258441499337983411',
                                                    'principalAmt'=>7000.0,
                                                    'penaltyAmt'=>0.0,
                                                    'interestAmt'=>6.26,
                                                    'serviceAmt'=>0.0,
                                                    'totalAmt'=>7006.26,
                                                    'resultRepayDate'=>'2019-03-28 18:39'
                                                ],
            'LO201903231702339369436564039884'=>[
                                                    'third_repay_id'=>'RR201903281840272021271363412617',
                                                    'principalAmt'=>15000.0,
                                                    'penaltyAmt'=>0.0,
                                                    'interestAmt'=>16.77,
                                                    'serviceAmt'=>0.0,
                                                    'totalAmt'=>15016.77,
                                                    'resultRepayDate'=>'2019-03-28 18:40'
                                                ],
            'LO201903231514549659839310363037'=>[
                                                    'third_repay_id'=>'RR201903281841219771826851648707',
                                                    'principalAmt'=>15000.0,
                                                    'penaltyAmt'=>0.0,
                                                    'interestAmt'=>16.77,
                                                    'serviceAmt'=>0.0,
                                                    'totalAmt'=>15016.77,
                                                    'resultRepayDate'=>'2019-03-28 18:41'
                                                ],
            'LO201903231002538679473091270494'=>[
                                                    'third_repay_id'=>'RR201903281842417151101048309235',
                                                    'principalAmt'=>15000.0,
                                                    'penaltyAmt'=>0.0,
                                                    'interestAmt'=>16.77,
                                                    'serviceAmt'=>0.0,
                                                    'totalAmt'=>15016.77,
                                                    'resultRepayDate'=>'2019-03-28 18:42'
                                                ],
            'LO201903221815427959817582064713'=>[
                                                    'third_repay_id'=>'RR201903281843290551672216535610',
                                                    'principalAmt'=>12000.0,
                                                    'penaltyAmt'=>0.0,
                                                    'interestAmt'=>16.1,
                                                    'serviceAmt'=>0.0,
                                                    'totalAmt'=>12016.1,
                                                    'resultRepayDate'=>'2019-03-28 18:43'
                                                ],
            'LO201903221600507339139994332422'=>[
                                                    'third_repay_id'=>'RR201903281844210351250978536748',
                                                    'principalAmt'=>1000.0,
                                                    'penaltyAmt'=>0.0,
                                                    'interestAmt'=>1.34,
                                                    'serviceAmt'=>0.0,
                                                    'totalAmt'=>1001.34,
                                                    'resultRepayDate'=>'2019-03-28 18:44'
                                                ],
            'LO201903221110255849479130491295'=>[
                                                    'third_repay_id'=>'RR201903281848077061775408640226',
                                                    'principalAmt'=>10000.0,
                                                    'penaltyAmt'=>0.0,
                                                    'interestAmt'=>13.41,
                                                    'serviceAmt'=>0.0,
                                                    'totalAmt'=>10013.41,
                                                    'resultRepayDate'=>'2019-03-28 18:48'
                                                ],
            'LO201903220137076809445641407819'=>[
                                                    'third_repay_id'=>'RR201903281848506931400371487649',
                                                    'principalAmt'=>12000.0,
                                                    'penaltyAmt'=>0.0,
                                                    'interestAmt'=>16.1,
                                                    'serviceAmt'=>0.0,
                                                    'totalAmt'=>12016.1,
                                                    'resultRepayDate'=>'2019-03-28 18:48'
                                                ],
            'LO201903211657348259605462169849'=>[
                                                    'third_repay_id'=>'RR201903281849390151645113256447',
                                                    'principalAmt'=>12000.0,
                                                    'penaltyAmt'=>0.0,
                                                    'interestAmt'=>18.78,
                                                    'serviceAmt'=>0.0,
                                                    'totalAmt'=>12018.78,
                                                    'resultRepayDate'=>'2019-03-28 18:49'
                                                ],
            'LO201903210731136089180945393003'=>[
                                                    'third_repay_id'=>'RR201903281850168571751551303089',
                                                    'principalAmt'=>15000.0,
                                                    'penaltyAmt'=>0.0,
                                                    'interestAmt'=>23.47,
                                                    'serviceAmt'=>0.0,
                                                    'totalAmt'=>15023.47,
                                                    'resultRepayDate'=>'2019-03-28 18:50'
                                                ],
            'LO201903202047069029102442699703'=>[
                                                    'third_repay_id'=>'RR201903281850539751430627504916',
                                                    'principalAmt'=>15000.0,
                                                    'penaltyAmt'=>0.0,
                                                    'interestAmt'=>26.83,
                                                    'serviceAmt'=>0.0,
                                                    'totalAmt'=>15026.83,
                                                    'resultRepayDate'=>'2019-03-28 18:50'
                                                ],
            'LO201903202026499939466759998251'=>[
                                                    'third_repay_id'=>'RR201903281851311441655306332156',
                                                    'principalAmt'=>15000.0,
                                                    'penaltyAmt'=>0.0,
                                                    'interestAmt'=>26.83,
                                                    'serviceAmt'=>0.0,
                                                    'totalAmt'=>15026.83,
                                                    'resultRepayDate'=>'2019-03-28 18:51'
                                                ],
            'LO201903201928206809378031021030'=>[
                                                    'third_repay_id'=>'RR201903281852090611544196225218',
                                                    'principalAmt'=>15000.0,
                                                    'penaltyAmt'=>0.0,
                                                    'interestAmt'=>26.83,
                                                    'serviceAmt'=>0.0,
                                                    'totalAmt'=>15026.83,
                                                    'resultRepayDate'=>'2019-03-28 18:52'
                                                ],
            'LO201903191343090279358882546903'=>[
                                                    'third_repay_id'=>'RR201903281853217281270762357469',
                                                    'principalAmt'=>15000.0,
                                                    'penaltyAmt'=>0.0,
                                                    'interestAmt'=>30.18,
                                                    'serviceAmt'=>0.0,
                                                    'totalAmt'=>15030.18,
                                                    'resultRepayDate'=>'2019-03-28 18:53'
                                                ],
            'LO201903181501408299766000603987'=>[
                                                    'third_repay_id'=>'RR201903281854562941436373301145',
                                                    'principalAmt'=>5000.0,
                                                    'penaltyAmt'=>0.0,
                                                    'interestAmt'=>11.18,
                                                    'serviceAmt'=>0.0,
                                                    'totalAmt'=>5011.18,
                                                    'resultRepayDate'=>'2019-03-28 18:54'
                                                ],
            'LO201903110002087099188122229898'=>[
                                                    'third_repay_id'=>'RR201903281855473151443886767124',
                                                    'principalAmt'=>15000.0,
                                                    'penaltyAmt'=>0.0,
                                                    'interestAmt'=>57.0,
                                                    'serviceAmt'=>0.0,
                                                    'totalAmt'=>15057.0,
                                                    'resultRepayDate'=>'2019-03-28 18:55'
                                                ],
            'LO201903100014366559134575073508'=>[
                                                    'third_repay_id'=>'RR201903281856520141586776939940',
                                                    'principalAmt'=>5000.0,
                                                    'penaltyAmt'=>0.0,
                                                    'interestAmt'=>20.12,
                                                    'serviceAmt'=>0.0,
                                                    'totalAmt'=>5020.12,
                                                    'resultRepayDate'=>'2019-03-28 18:56'
                                                ],
            'LO201903090017087329179842877794'=>[
                                                    'third_repay_id'=>'RR201903281857472571188342819857',
                                                    'principalAmt'=>12000.0,
                                                    'penaltyAmt'=>0.0,
                                                    'interestAmt'=>50.97,
                                                    'serviceAmt'=>0.0,
                                                    'totalAmt'=>12050.97,
                                                    'resultRepayDate'=>'2019-03-28 18:57'
                                                ],
            'LO201903060157085019329264648216'=>[
                                                    'third_repay_id'=>'RR201903281858285951303427966257',
                                                    'principalAmt'=>12000.0,
                                                    'penaltyAmt'=>0.0,
                                                    'interestAmt'=>59.02,
                                                    'serviceAmt'=>0.0,
                                                    'totalAmt'=>12059.02,
                                                    'resultRepayDate'=>'2019-03-28 18:58'
                                                ],
            'LO201903302102171249422392597143'=>[
                                                    'third_repay_id'=>'RR201904042030275301198797682146',
                                                    'principalAmt'=>12000.0,
                                                    'penaltyAmt'=>0.0,
                                                    'interestAmt'=>13.41,
                                                    'serviceAmt'=>0.0,
                                                    'totalAmt'=>12013.41,
                                                    'resultRepayDate'=>'2019-04-04 20:30'
                                                ],
            'LO201904011146404039717224125956'=>[
                                                    'third_repay_id'=>'RR201904042037116631760055838523',
                                                    'principalAmt'=>3000.0,
                                                    'penaltyAmt'=>0.0,
                                                    'interestAmt'=>2.01,
                                                    'serviceAmt'=>0.0,
                                                    'totalAmt'=>3002.01,
                                                    'resultRepayDate'=>'2019-04-04 20:37'
                                                ],
            'LO201903300801580189344457570134'=>[
                                                    'third_repay_id'=>'RR201904042039279061753262262734',
                                                    'principalAmt'=>7000.0,
                                                    'penaltyAmt'=>0.0,
                                                    'interestAmt'=>7.82,
                                                    'serviceAmt'=>0.0,
                                                    'totalAmt'=>7007.82,
                                                    'resultRepayDate'=>'2019-04-04 20:39'
                                                ],
            'LO201904011526260269469094584834'=>[
                                                    'third_repay_id'=>'RR201904042041203551514623548355',
                                                    'principalAmt'=>2000.0,
                                                    'penaltyAmt'=>0.0,
                                                    'interestAmt'=>1.34,
                                                    'serviceAmt'=>0.0,
                                                    'totalAmt'=>2001.34,
                                                    'resultRepayDate'=>'2019-04-04 20:41'
                                                ],
            'LO201904031022595619662419054027'=>[
                                                    'third_repay_id'=>'RR201904042043462131301042299047',
                                                    'principalAmt'=>12000.0,
                                                    'penaltyAmt'=>0.0,
                                                    'interestAmt'=>2.68,
                                                    'serviceAmt'=>0.0,
                                                    'totalAmt'=>12002.68,
                                                    'resultRepayDate'=>'2019-04-04 20:43'
                                                ],
            'LO201903221742085359435301398007'=>[
                                                    'third_repay_id'=>'RR201904042045316321128936283156',
                                                    'principalAmt'=>2000.0,
                                                    'penaltyAmt'=>0.0,
                                                    'interestAmt'=>5.81,
                                                    'serviceAmt'=>0.0,
                                                    'totalAmt'=>2005.81,
                                                    'resultRepayDate'=>'2019-04-04 20:45'
                                                ],
            'LO201903241842435469183976182540'=>[
                                                    'third_repay_id'=>'RR201904042048321021716328492076',
                                                    'principalAmt'=>7000.0,
                                                    'penaltyAmt'=>0.0,
                                                    'interestAmt'=>17.21,
                                                    'serviceAmt'=>0.0,
                                                    'totalAmt'=>7017.21,
                                                    'resultRepayDate'=>'2019-04-04 20:48'
                                                ],
            'LO201902121535256049880751718010'=>[
                                                    'third_repay_id'=>'RR201904042048532001388272128199',
                                                    'principalAmt'=>12671.31,
                                                    'penaltyAmt'=>253.43,
                                                    'interestAmt'=>275.25,
                                                    'serviceAmt'=>0.0,
                                                    'totalAmt'=>13199.99,
                                                    'resultRepayDate'=>'2019-04-04 20:48'
                                                ],
            'LO201903241318078639484467146517'=>[
                                                    'third_repay_id'=>'RR201904051142556341456657755212',
                                                    'principalAmt'=>5000.0,
                                                    'penaltyAmt'=>0.0,
                                                    'interestAmt'=>13.41,
                                                    'serviceAmt'=>0.0,
                                                    'totalAmt'=>5013.41,
                                                    'resultRepayDate'=>'2019-04-05 11:42'
                                                ],
            'LO201903051537366439296852760353'=>[
                                                    'third_repay_id'=>'RR201904082105325811402539084280',
                                                    'principalAmt'=>8361.06,
                                                    'penaltyAmt'=>0.0,
                                                    'interestAmt'=>5.61,
                                                    'serviceAmt'=>0.0,
                                                    'totalAmt'=>8366.67,
                                                    'resultRepayDate'=>'2019-04-08 21:05'
                                                ],
            'LO201903130755509299638979411575'=>[
                                                    'third_repay_id'=>'RR201904082106589931445944774790',
                                                    'principalAmt'=>1672.21,
                                                    'penaltyAmt'=>0.0,
                                                    'interestAmt'=>0.0,
                                                    'serviceAmt'=>0.0,
                                                    'totalAmt'=>1672.21,
                                                    'resultRepayDate'=>'2019-04-08 21:06'
                                                ],
            'LO201904031550132969716639391851'=>[
                                                    'third_repay_id'=>'RR201904092030173611623190079615',
                                                    'principalAmt'=>7000.0,
                                                    'penaltyAmt'=>0.0,
                                                    'interestAmt'=>9.39,
                                                    'serviceAmt'=>0.0,
                                                    'totalAmt'=>7009.39,
                                                    'resultRepayDate'=>'2019-04-09 20:30'
                                                ],
            'LO201904050934527119216601811179'=>[
                                                    'third_repay_id'=>'RR201904092029482271639094646063',
                                                    'principalAmt'=>12000.0,
                                                    'penaltyAmt'=>0.0,
                                                    'interestAmt'=>10.73,
                                                    'serviceAmt'=>0.0,
                                                    'totalAmt'=>12010.73,
                                                    'resultRepayDate'=>'2019-04-09 20:29'
                                                ],
            'LO201903131100455919657678300767'=>[
                                                    'third_repay_id'=>'RR201904092029074491554579790927',
                                                    'principalAmt'=>12000.0,
                                                    'penaltyAmt'=>0.0,
                                                    'interestAmt'=>72.43,
                                                    'serviceAmt'=>0.0,
                                                    'totalAmt'=>12072.43,
                                                    'resultRepayDate'=>'2019-04-09 20:29'
                                                ],
            'LO201904051343060089695875705638'=>[
                                                    'third_repay_id'=>'RR201904091930072961764055029450',
                                                    'principalAmt'=>10033.27,
                                                    'penaltyAmt'=>0.0,
                                                    'interestAmt'=>0.0,
                                                    'serviceAmt'=>0.0,
                                                    'totalAmt'=>10033.27,
                                                    'resultRepayDate'=>'2019-04-09 19:30'
                                                ],
            'LO201904021719149479650385675072'=>[
                                                    'third_repay_id'=>'RR201904091931386181843783670271',
                                                    'principalAmt'=>15000.0,
                                                    'penaltyAmt'=>0.0,
                                                    'interestAmt'=>23.47,
                                                    'serviceAmt'=>0.0,
                                                    'totalAmt'=>15023.47,
                                                    'resultRepayDate'=>'2019-04-09 19:31'
                                                ],
            'LO201904041800506189881396992277'=>[
                                                    'third_repay_id'=>'RR201904091933338961491970878813',
                                                    'principalAmt'=>10000.0,
                                                    'penaltyAmt'=>0.0,
                                                    'interestAmt'=>11.18,
                                                    'serviceAmt'=>0.0,
                                                    'totalAmt'=>10011.18,
                                                    'resultRepayDate'=>'2019-04-09 19:33'
                                                ],

            'LO201903051404228739534801671144'=>[
                                                    'third_repay_id'=>'RR201904051503421401564019980084',
                                                    'principalAmt'=>4000.0,
                                                    'penaltyAmt'=>0.0,
                                                    'interestAmt'=>27.72,
                                                    'serviceAmt'=>0.0,
                                                    'totalAmt'=>4027.72,
                                                    'resultRepayDate'=>'2019-04-05 15:03'
                                                ],
            'LO201903070950263289764028759401'=>[
                                                    'third_repay_id'=>'RR201904051509042841669057216370',
                                                    'principalAmt'=>5852.74,
                                                    'penaltyAmt'=>0.0,
                                                    'interestAmt'=>0.0,
                                                    'serviceAmt'=>0.0,
                                                    'totalAmt'=>5852.74,
                                                    'resultRepayDate'=>'2019-04-05 15:09'
                                                ],
            'LO201903211656192719415172255114'=>[
                                                    'third_repay_id'=>'RR201904051444387741572615903155',
                                                    'principalAmt'=>15000.0,
                                                    'penaltyAmt'=>0.0,
                                                    'interestAmt'=>50.3,
                                                    'serviceAmt'=>0.0,
                                                    'totalAmt'=>15050.3,
                                                    'resultRepayDate'=>'2019-04-05 14:44'
                                                ],
            'LO201903240252473829101277281447'=>[
                                                    'third_repay_id'=>'RR201904051445363671583838232467',
                                                    'principalAmt'=>15000.0,
                                                    'penaltyAmt'=>0.0,
                                                    'interestAmt'=>40.24,
                                                    'serviceAmt'=>0.0,
                                                    'totalAmt'=>15040.24,
                                                    'resultRepayDate'=>'2019-04-05 14:45'
                                                ],
            'LO201903261353136209138598317739'=>[
                                                    'third_repay_id'=>'RR201904051512284971179164260430',
                                                    'principalAmt'=>12000.0,
                                                    'penaltyAmt'=>0.0,
                                                    'interestAmt'=>26.83,
                                                    'serviceAmt'=>0.0,
                                                    'totalAmt'=>12026.83,
                                                    'resultRepayDate'=>'2019-04-05 15:12'
                                                ],
            'LO201903060157020919852987991505'=>[
                                                    'third_repay_id'=>'RR201904071953450751492612553055',
                                                    'principalAmt'=>5852.74,
                                                    'penaltyAmt'=>0.0,
                                                    'interestAmt'=>1.31,
                                                    'serviceAmt'=>0.0,
                                                    'totalAmt'=>5854.05,
                                                    'resultRepayDate'=>'2019-04-07 19:53'
                                                ],

            'LO201903211415527709517131115873'=>[
                                                    'third_repay_id'=>'RR201904051520137051578679413899',
                                                    'principalAmt'=>4682.24,
                                                    'penaltyAmt'=>0.0,
                                                    'interestAmt'=>0.0,
                                                    'serviceAmt'=>0.0,
                                                    'totalAmt'=>4682.24,
                                                    'resultRepayDate'=>'2019-04-05 15:20'
                                                ],
            'LO201903291435531199670503805417'=>[
                                                    'third_repay_id'=>'RR201904051500280811718228188811',
                                                    'principalAmt'=>5852.74,
                                                    'penaltyAmt'=>0.0,
                                                    'interestAmt'=>0.0,
                                                    'serviceAmt'=>0.0,
                                                    'totalAmt'=>5852.74,
                                                    'resultRepayDate'=>'2019-04-05 15:00'
                                                ],
            'LO201903291817254129557834811857'=>[
                                                    'third_repay_id'=>'RR201904051449548691377719441877',
                                                    'principalAmt'=>8000.0,
                                                    'penaltyAmt'=>0.0,
                                                    'interestAmt'=>12.52,
                                                    'serviceAmt'=>0.0,
                                                    'totalAmt'=>8012.52,
                                                    'resultRepayDate'=>'2019-04-05 14:49'
                                                ],
            'LO201904011505314729682329608119'=>[
                                                    'third_repay_id'=>'RR201904051446397581131298188571',
                                                    'principalAmt'=>12541.58,
                                                    'penaltyAmt'=>0.0,
                                                    'interestAmt'=>0.0,
                                                    'serviceAmt'=>0.0,
                                                    'totalAmt'=>12541.58,
                                                    'resultRepayDate'=>'2019-04-05 14:46'
                                                ],
            'LO201903301855123069436072011572'=>[
                                                    'third_repay_id'=>'RR201904051447521121853010771805',
                                                    'principalAmt'=>15000.0,
                                                    'penaltyAmt'=>0.0,
                                                    'interestAmt'=>20.12,
                                                    'serviceAmt'=>0.0,
                                                    'totalAmt'=>15020.12,
                                                    'resultRepayDate'=>'2019-04-05 14:47'
                                                ],
            'LO201904011745344869687915972568'=>[
                                                    'third_repay_id'=>'RR201904051458207971478349333524',
                                                    'principalAmt'=>5000.0,
                                                    'penaltyAmt'=>0.0,
                                                    'interestAmt'=>4.47,
                                                    'serviceAmt'=>0.0,
                                                    'totalAmt'=>5004.47,
                                                    'resultRepayDate'=>'2019-04-05 14:58'
                                                ],
            'LO201904011527393889162665822181'=>[
                                                    'third_repay_id'=>'RR201904051459270311820501503245',
                                                    'principalAmt'=>10033.27,
                                                    'penaltyAmt'=>0.0,
                                                    'interestAmt'=>0.0,
                                                    'serviceAmt'=>0.0,
                                                    'totalAmt'=>10033.27,
                                                    'resultRepayDate'=>'2019-04-05 14:59'
                                                ],
            'LO201903311710533519240119952073'=>[
                                                    'third_repay_id'=>'RR201904051455188621519658532273',
                                                    'principalAmt'=>7000.0,
                                                    'penaltyAmt'=>0.0,
                                                    'interestAmt'=>7.82,
                                                    'serviceAmt'=>0.0,
                                                    'totalAmt'=>7007.82,
                                                    'resultRepayDate'=>'2019-04-05 14:55'
                                                ],
        ];
        $no_oper=[];
        $now=Carbon::now()->format('Y-m-d H:i:s');
        foreach($data as $k=>$v){
            //获取全部
            $info=RepaySummary::leftJoin('repay_records as a','a.summary_id','repay_summaries.id')
                ->where('repay_summaries.loan_id',$k)
                ->select(
                    'a.uid as uid',
                    'a.summary_id as summary_id',
                    'a.bank_cards_id as bank_cards_id',
                    'a.normal_date as normal_date',
                    'a.id as record_id',
                    'a.amount as record_amount',
                    'a.pay_off as pay_off',
                    'a.repay_type as repay_type',
                    'a.repay_date as repay_date',
                    'repay_summaries.period_count as period_count'
                )
                ->get();
        //    $last_info=$info[0]['period_count'];
            $position=count($info)-1;
//            //获取最后一期
            $last_info=RepayRecord::where('id',$info[$position]['record_id'])->select('uid','summary_id','bank_cards_id','normal_date','pay_off','repay_type','repay_date')->first();
            //本条记录全部结清的费用
            $record_money=0;
            foreach ($info as $key=>$val){
                if($val->pay_off==2){
                    $record_money+=$val['record_amount'];
                    $record_data_before['repay_type']=7;
                    RepayRecord::where('id',$val->record_id)->update($record_data_before);
                }elseif ($val->repay_type==7){
                    $record_money+=$val['record_amount'];
                    $record_data_before['pay_off']=2;
                    RepayRecord::where('id',$val->record_id)->update($record_data_before);
                }else{
                    if($val->repay_date==$last_info->repay_date){
                        $record_money+=$val['record_amount'];
                        $record_data_before['pay_off']=2;
                        $record_data_before['repay_type']=7;
                        RepayRecord::where('id',$val->record_id)->update($record_data_before);
                    }
                }
            }
            //生成record表0的记录
            $record_data=[
                'uid'=>$last_info->uid,
                'summary_id'=>$last_info->summary_id,
                'bank_cards_id'=>$last_info->bank_cards_id,
                'period'=>0,
                'amount'=>$record_money,
                'status'=>4,
                'pay_off'=>2,
                'repay_type'=>7,
                'xz_status'=>0,
                'normal_date'=>$last_info->normal_date,
                'created_at'=>Carbon::now()->format('Y-m-d H:i:s'),
                'updated_at'=>Carbon::now()->format('Y-m-d H:i:s'),
                'repay_date'=>$last_info->repay_date
            ];
            $record_id=RepayRecord::insertGetId($record_data);
            //生成money表0的记录
            $money_data=[
                'uid'       =>$last_info->uid,
                'summary_id'=>$last_info->summary_id,
                'record_id' =>$record_id,
                'bank_cards_id'=>$last_info->bank_cards_id,
                'period'=>0,
                'third_repay_id' => $v['third_repay_id'],
                'trade_no' => '',
                'lending_money' => bcmul($v['interestAmt'],100),
                'fines_money' => bcmul($v['penaltyAmt'],100),
                'repayment_money' => bcmul($v['serviceAmt'],100),
                'failure'=>'',
                'status'=>4,
                'type' => 4,
                'status_time'=>$v['resultRepayDate'],
                'repay_type'=>7,
                'created_at'=>$now,
                'repay_date'=>$last_info->repay_date
             //   'updated_at'=>Carbon::now()->format('Y-m-d H:i:s'), 
            ];
            RepayRecordMoney::create($money_data);
            //生成month表 0的记录
            $month_data=[
                'uid'=>$last_info->uid,
                'summary_id'=>$last_info->summary_id,
                'record_id' =>$record_id,
                'bank_cards_id'=>$last_info->bank_cards_id,
                'amount'=>0,
                'period'=>0,
                'status'=>4,
                'status_time'=>$v['resultRepayDate'],
                'repay_type'=>7,
                'created_at'=>Carbon::now()->format('Y-m-d H:i:s'),
                'updated_at'=>Carbon::now()->format('Y-m-d H:i:s'),
            ];
            RepayRecordMonth::create($month_data);

            $summary_data=RepaySummary::where('id',$last_info->summary_id)->first();
            $moneyData=$this->moneyData($last_info->summary_id,$summary_data->loan_id,$last_info);
            $common_info=$this->commonData($last_info->summary_id,$summary_data,$last_info,$moneyData);
            //生成common表
            $common_data=[
                'uid'       =>$last_info->uid,
                'summary_id'=>$last_info->summary_id,
                'record_id' =>$record_id,
                'bank_cards_id'=>$last_info->bank_cards_id,
                'third_record_id' => '',
                'trade_no' => '',
                'period'=>0,
                'lending_money' => 0,
                'fines_money' => 0,
                'repayment_money'=>$common_info['repayment_money'],
                'status'=>4,
                'status_time'=>$now,
                'repay_type'=>7,
                'created_at'=>$now,
            //    'updated_at'=>$now, 
            ];
            RepayRecordCommon::create($common_data);
            //修改其他期状态
            Repay::setAllPeriodSuccess($last_info, RepayRecord::REPAY_TYPE_XIANXIA, false);
            //同步催收
            event(new RepayMentEvent($last_info->id));
        }
        Log::debug(json_encode($no_oper,JSON_UNESCAPED_UNICODE));
        $this->info('success');
    }

    /**
     * 生成money的数据
     */
    public function moneyData($summary_id,$loan_id,$record){
        //农信的和光大的金额为0
        $data =$this->getRepaymentPlan($loan_id,$summary_id);
        $recordMoney['uid'] = $record->uid;
        $recordMoney['summary_id'] = $summary_id;
        $recordMoney['record_id'] = $record->id;
        $recordMoney['bank_cards_id'] = 0;
        $recordMoney['trade_no'] = 0;
        $recordMoney['period'] = 0;
        $recordMoney['lending_money'] = $data['lending_money']; //利息
        $recordMoney['fines_money'] = $data['fines_money']; //罚息
        $recordMoney['repayment_money'] = $data['repayment_money']; //手续费
        $is_channel = $this->getLoanChannel($summary_id);
        if(false == $is_channel){
            //status = 4
            $recordMoney['status'] = RepayRecordMoney::STATUS_FOUR;
        }
        return $recordMoney;
    }
    /**
     * 通过loan_id获取提前还清的金额信息
     */
    public function getRepaymentPlan($loan_id,$summary_id){
        $is_channel = self::getLoanChannel($summary_id);
        if(false == $is_channel){
            return [
                'lending_money' => 0,
                'fines_money' => 0,
                'repayment_money' => 0,
            ];
        }
        $res = MoneyPlatformRequestFacade::getRepaymentPlan($loan_id);
        $data = isset($res['data']) ? $res['data']:[];
        $returnData['lending_money'] = isset($data['interestAmount']) ? $data['interestAmount'] : 0; //利息
        $returnData['fines_money'] = isset($data['penaltyAmount']) ? $data['penaltyAmount'] : 0; //罚息
        $returnData['repayment_money'] = isset($data['serviceAmount']) ? $data['serviceAmount'] : 0; //手续费
        $returnData['amount'] = isset($data['principalAmount']) ? $data['principalAmount'] : 0; //提前还款本金
        return $returnData;
    }
    /**
     * Description: 获取是否为晋商资方渠道
     * Author: Gxs
     * @param $summary_id
     * @return bool true 晋商, false 其他
     */
    public function getLoanChannel($summary_id)
    {
        $summary_model = RepaySummary::find($summary_id);
        if(RepaySummary::DATA_TYPE_ONE == $summary_model->data_type){
            return true;
        }
        if ('LCC201709190010' == $summary_model->loan_channel)
            return true; //晋商 || 新光大
        else
            return false;
    }
    /**
     * common数据
     */
    public function commonData($summary_id,$repayInfo,$record,$moneyData){
        $recordCommon['uid'] = $record->uid;
        $recordCommon['summary_id'] = $summary_id;
        $recordCommon['record_id'] = $record->id;
        $recordCommon['bank_cards_id'] = 0;
        $recordCommon['trade_no'] = 0;
        $recordCommon['period'] = 0;
        //计算提前还清手续费
        $info = Repay::repaymentMoney($repayInfo->period,$summary_id);
        $info = bcsub($info,$moneyData['repayment_money'],2);//合计-资方的=公共的
        //2019年2.27 确认 公共的手续费要减去资方提前还清的利息 罚息 手续费  才是公共的手续费
        $info = bcsub($info,$moneyData['lending_money'],2);
        $info = bcsub($info,$moneyData['fines_money'],2);
        $recordCommon['repayment_money'] = $info;
        return $recordCommon;
    }
}